<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
global $dal;

include( '../global/dlog.php' );
include( '../configuration/config.php' );
include( '../global/old_database_connection.php');
include( '../global/DatabaseAbstractionLayer.php');
include( '../global/utils.php' );
include( '../global/translations.php' );
$dal = new DatabaseAbstractionLayer();
// Set the database abstraction layer to use the same debug setting as used in the config
$dal->setDebug( DEBUG );

global $language;
$language = 'en';
header( 'Content-type: text/html; charset=utf-8' );
$query5 = "SELECT language, title " .
		"FROM languages " .
		"ORDER BY default_language DESC, title ASC";
$result5 = mysql_query( $query5 ) or die( "Query failed : " . mysql_error() );
$num5 = mysql_numrows( $result5 );
if( isset( $_GET[ 'language' ] ) ) {
	$i = 0;
	while ( $i < $num5 ) {
		if( $_GET[ 'language' ] == mysql_result( $result5, $i, 'language' ) ) {
			$language = mysql_result( $result5, $i, 'language' );
			break;
		}
		$i ++;
	}
}

if( isset( $_GET[ 'code' ] ) ) {
	$statement = $dal->prepare( "SELECT username " .
			"FROM users " .
			"WHERE `activation_code` = :code" );
	$statement->bindParam( ':code', $_GET[ 'code' ], PDO::PARAM_STR );
	$username = $dal->getValue( $statement );
	
	if( isset( $username ) ) {
		$verified = true;
		$statement = $dal->prepare( "UPDATE users " .
				"SET `verified` = 1, `activation_code` = '' " .
				"WHERE `activation_code`  = :code" );
		$new_code = '';
		$statement->bindParam( ':code', $_GET[ 'code' ], PDO::PARAM_STR );
		$dal->execute( $statement );

		// Added
		$query = "SELECT user_id, password " .
				"FROM users WHERE username = '" . $username . "'";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			
		session_start();
		session_set_cookie_params ( 0 );
		$_SESSION[ 'login' ] = 1;
		$_SESSION[ 'password' ] = $row[ 'password' ];
		$_SESSION[ 'user_id' ] = $row[ 'user_id' ];
		$_SESSION[ 'user_just_registered' ] = 1;
		
		header( 'Location: sign_up.php' );
	}
	else {
		$verified = false;
	}
}
else {
	$verified = false;
}

if( !$verified ) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<title>Ages of Strife: <?php transprint( 'Email verification' ); ?></title>
		<link rel="stylesheet" href="agesofstrife.css"></link>
		<script language="JavaScript" type="text/javascript" src="global.js"></script>
	</head>
	<body>
		<div id="root_container">
			<div align="center" id="header-image">
				<a href="../index.php?language=<?php print( $language ); ?>">
					<img class="header-dummy-image" src="../resources/dummy.gif" alt="Ages of Strife" border="0" />
				</a>
			</div>
<?php
include( 'ads.php' );
?>
			<div id="container">
				<div id="main-content">
					<h2><?php transprint( 'Register' ); ?></h2><br />
					<br />
					<div class="blok">
						<?php transprint( 'Language' ); ?>:&nbsp;
<?php
	$i = 0;
	while ( $i < $num5 ) {
		if( $i > 0 ) {
			print( ', ' );
		}
		if( $language == mysql_result( $result5, $i, 'language' ) ) {
			print( '<strong>' . mysql_result( $result5, $i, 'title' ) . ' (' . mysql_result( $result5, $i, 'language' ) . ')</strong>' );
		}
		else {
			print( '<a href="email_verification.php?language=' . mysql_result( $result5, $i, 'language' ) . '">' . mysql_result( $result5, $i, 'title' ) . ' (' . mysql_result( $result5, $i, 'language' ) . ')</a>' );
		}
		$i ++;
	}
?>
					</div>
					<br />
					<div class="blok">
						<strong><?php transprint( 'Email verification' ); ?></strong><br />
						<?php transprint( 'You\'ve been sent a confirmation e-mail which contains an activation code, either click on the link in the e-mail or copy the code here.' ); ?><br />
						<br />
<?php
	$activationCode = '';
	if( isset( $_GET[ 'code' ] ) ) {
		$activationCode = $_GET[ 'code' ];
?>
						<strong><?php transprint( 'Status' ); ?></strong>: <?php transprint( 'The entered activation code is not correct.' ); ?><br />
						<br />
<?php		
	}
?>
						<strong><?php transprint( 'Email verification' ); ?></strong><br />
						<form method="get" action="email_verification.php">
							<table>
								<tr>
									<td><?php transprint( 'Code' ); ?>&nbsp;</td>
									<td><input type="text" name="code" size="40" value="<?php print( $activationCode )?>" /></td>
								</tr>
							</table>
							<input type="submit" value="<?php transprint( 'Confirm' ); ?>" /><br />
						</form>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
<?php 
}
?>